{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": true,
    "pycharm": {
     "name": "#%% md\n"
    }
   },
   "source": [
    "# 通用物体和场景识别-高级版组件\n",
    "\n",
    "## 目标\n",
    "使用通用物体和场景识别-高级版组件对图片上的物品或场景进行识别。\n",
    "\n",
    "## 准备工作\n",
    "### 平台注册\n",
    "1.先在appbuilder平台注册，获取token\n",
    "\n",
    "2.安装appbuilder-sdk"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [
    "!pip install appbuilder-sdk"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "## 基本用法\n",
    "\n",
    "### 快速开始\n",
    "\n",
    "以下是一个简单的例子来演示如何开始使用ObjectRecognition组件："
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [
    "import os\n",
    "import appbuilder\n",
    "import requests\n",
    "\n",
    "# 请前往千帆AppBuilder官网创建密钥，流程详见：https://cloud.baidu.com/doc/AppBuilder/s/Olq6grrt6#1%E3%80%81%E5%88%9B%E5%BB%BA%E5%AF%86%E9%92%A5\n",
    "os.environ[\"APPBUILDER_TOKEN\"] = '...'\n",
    "\n",
    "# 从BOS读取样例图片\n",
    "image_url = \"https://bj.bcebos.com/v1/appbuilder/object_recognize_test.png?\"\\\n",
    "    \"authorization=bce-auth-v1%2FALTAKGa8m4qCUasgoljdEDAzLm%2F2024-01-\"\\\n",
    "    \"11T11%3A00%3A19Z%2F-1%2Fhost%2F2c31bf29205f61e58df661dc80af31a1dc\"\\\n",
    "    \"1ba1de0a8f072bc5a87102bd32f9e3\"\n",
    "raw_image = requests.get(image_url).content\n",
    "# 创建物体识别组件实例\n",
    "object_recognition = appbuilder.ObjectRecognition()\n",
    "# 执行识别操作并获取结果\n",
    "out = object_recognition.run(appbuilder.Message(content={\"raw_image\": raw_image}))\n",
    "print(out.content)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "## 参数说明\n",
    "\n",
    "### 鉴权说明\n",
    "使用组件之前，请首先申请并设置鉴权参数，可参考[组件使用流程](https://cloud.baidu.com/doc/AppBuilder/s/Olq6grrt6#1%E3%80%81%E5%88%9B%E5%BB%BA%E5%AF%86%E9%92%A5)。\n",
    "```python\n",
    "# 设置环境中的TOKEN，以下示例略\n",
    "os.environ[\"APPBUILDER_TOKEN\"] = \"bce-YOURTOKEN\"\n",
    "```\n",
    "\n",
    "### 初始化参数\n",
    "无\n",
    "\n",
    "### 调用参数\n",
    "\n",
    "| 参数名称    | 参数类型    | 是否必须 | 描述                          | 示例值                                            |\n",
    "|---------|---------|------|-----------------------------|------------------------------------------------|\n",
    "| message | String  | 是    | 输入的消息，用于模型的主要输入内容。这是一个必需的参数 | Message(content={\"raw_image\": b\"待识别的图片字节流数据\"}) |\n",
    "|timeout| Float   | 否    | HTTP超时时间,单位：秒               |1|\n",
    "| retry   | Integer | 否    | HTTP重试次数                    | 3                                              |\n",
    "\n",
    "### 响应参数\n",
    "| 参数名称     | 参数类型    | 描述          | 示例值                                                 |\n",
    "|----------|---------|-------------|-----------------------------------------------------|\n",
    "| result   | Array[] | 返回结果        | [{\"keyword\":\"苹果\",\"score\":0.961247,\"root\":\"植物-蔷薇科\"}] |\n",
    "| +keyword | String  | 图片中的物体或场景名称 | \"苹果\"                                                |\n",
    "| +score\t  | Float   | 置信度         | 0.961247                                            |\n",
    "| +root\t   | String  | 识别结果的上层标签   | \"植物-蔷薇科\"                                            |\n",
    "\n",
    "\n",
    "### 响应示例\n",
    "#### 示例图片\n",
    "![示例图片](./image/object_recognize示例.png)\n",
    "#### 识别结果\n",
    "```json\n",
    "{\n",
    "    \"result\":[\n",
    "        {\n",
    "            \"keyword\":\"苹果\",\n",
    "            \"score\":0.961247,\n",
    "            \"root\":\"植物-蔷薇科\"\n",
    "        },\n",
    "        {\n",
    "            \"keyword\":\"姬娜果\",\n",
    "            \"score\":0.740838,\n",
    "            \"root\":\"植物-其它\"\n",
    "        },\n",
    "        {\n",
    "            \"keyword\":\"梨子\",\n",
    "            \"score\":0.392218,\n",
    "            \"root\":\"商品-水果\"\n",
    "        },\n",
    "        {\n",
    "            \"keyword\":\"车厘子\",\n",
    "            \"score\":0.193986,\n",
    "            \"root\":\"植物-其它\"\n",
    "        },\n",
    "        {\n",
    "            \"keyword\":\"石榴\",\n",
    "            \"score\":0.000239,\n",
    "            \"root\":\"植物-千屈菜科\"\n",
    "        }\n",
    "    ]\n",
    "}\n",
    "```\n",
    "\n",
    "\n"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%% md\n"
    }
   }
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
